#include "register.h"
#include "ui_register.h"
#include "dialog.h"

#include<iostream>
#include<fstream>

Register::Register(QWidget *parent,Dialog* s)
    : QDialog(parent)
    , ui(new Ui::Register)
{
    dialog=s;
    ui->setupUi(this);
}

Register::~Register()
{
    delete dialog;
    delete ui;
}

void Register::on_pushButton_clicked()
{
    string addusername=ui->lineEdit_User->text().toStdString();
    string addpasswd=ui->lineEdit_passwd->text().toStdString();
    string addconfirm_psd=ui->lineEdit_passwd_2->text().toStdString();

    //判断登录的用户是管理员还是消费者
    string currentAdmin = ui->comboBox->currentText().toStdString();
    bool isAdmin=currentAdmin=="admin"?1:0;

    auto usermanager=dialog->getUserManager();

    QMessageBox msgBOX;
    msgBOX.setWindowTitle("Register");
    //判断用户名是否存在或合法
    if(!usermanager->isUsernameValid(addusername))
    {
        msgBOX.setText("Username is invalid or already exists.");
        msgBOX.setStandardButtons(QMessageBox::Ok);
        msgBOX.exec();
    }
    else
    {
        //判断密码强度是否太弱
        if(!usermanager->isPasswordStrong(addpasswd))
        {
            msgBOX.setText("Password is too weak.");
            msgBOX.setStandardButtons(QMessageBox::Ok);
            msgBOX.exec();
        }
        else
        {
            //判断再次输入的密码是否正确
            if(addconfirm_psd!=addpasswd)
            {
                msgBOX.setText("Passwords do not match.");
                msgBOX.setStandardButtons(QMessageBox::Ok);
                msgBOX.exec();
            }
            else
            {
                usermanager->returnUsers().insert(make_pair(addusername,User(addusername,addpasswd,isAdmin)));

                for(auto& s:usermanager->returnUsers())
                {
                    cout<<s.first<<endl;
                }

                msgBOX.setText("Registration successful!");
                msgBOX.setStandardButtons(QMessageBox::Ok);
                msgBOX.exec();
                this->close();

                //将创建好的用户加入到文件里面
                QString fileName = "D:/QT_project/Restaurant/users.txt"; // 文件路径

                QFile file(fileName);
                if (!file.open(QIODevice::Append | QIODevice::Text)) {
                    qDebug() << "Failed to open file:" << file.errorString();
                    return;
                }

                QTextStream out(&file);

                // 假设caesarcipher是你的凯撒加密对象，addusername是要加密的用户名，addpasswd是要加密的密码
                QString encryptedUsername = caesarcipher->Encrypt(addusername, caesarcipher->getKey()).c_str();
                QString encryptedPassword = caesarcipher->Encrypt(addpasswd, caesarcipher->getKey()).c_str();

                QString isAdmin1 = (1==isAdmin)? "1" : "0"; // 假设isAdmin是一个bool变量表示是否为管理员

                out << "username:" << encryptedUsername << "  password:" << encryptedPassword << " " << isAdmin1 <<"\n";

                file.close();
            }
        }

    }

    ui->lineEdit_User->clear();
    ui->lineEdit_passwd->clear();
    ui->lineEdit_passwd_2->clear();


}




